﻿Imports Models.Security
Imports Models.Users

Namespace Security

    Public Class Permissions
        Private permissionList As Generic.List(Of PermissionVO)

        Sub New()
            permissionList = New Generic.List(Of PermissionVO)

            Dim permission As New PermissionVO
            permission.code = "OWN"
            permissionList.Add(permission)

            permission = New PermissionVO
            permission.code = "R"
            permissionList.Add(permission)

            permission = New PermissionVO
            permission.code = "W"
            permissionList.Add(permission)

            permission = New PermissionVO
            permission.code = "RW"
            permissionList.Add(permission)

        End Sub

        ''' <summary>
        ''' Sincronize Permissions
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub sincronize()

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")


                '' add/update all Permissions
                Dim dbPermList As Generic.List(Of Data.BaseModel.Permission) = (From r In oDB.Permissions Select r).ToList
                Dim found As Boolean
                For Each perm As PermissionVO In permissionList

                    found = False

                    '' locate Permission in db and update it
                    '' Comentado porque no hay datos extras por ahora
                    For Each dbPerm As Data.BaseModel.Permission In dbPermList
                        If dbPerm.Code = perm.code Then
                            'dbRol.Name = rol.name
                            found = True
                        End If
                    Next

                    '' create if not found
                    If Not found Then
                        Dim entity As New Data.BaseModel.Permission

                        entity.Code = perm.code

                        oDB.Permissions.AddObject(entity)
                    End If
                Next

                oDB.SaveChanges()

            End Using
        End Sub

    End Class
End Namespace